package cc.jq1024.domain.award.service;

import cc.jq1024.domain.award.event.SendAwardMessageEvent;
import cc.jq1024.domain.award.model.aggregate.UserAwardRecordAggregate;
import cc.jq1024.domain.award.model.entity.DistributeAwardEntity;
import cc.jq1024.domain.award.model.entity.TaskEntity;
import cc.jq1024.domain.award.model.entity.UserAwardRecordEntity;
import cc.jq1024.domain.award.model.valobj.TaskStateVO;
import cc.jq1024.domain.award.repository.IAwardRepository;
import cc.jq1024.domain.award.service.distribute.IDistributeAward;
import cc.jq1024.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 奖品服务接口 - 实现
 * @Date: 2024-06-20-19:50
 */
@Slf4j
@Service
public class AwardService implements IAwardService {

    private final IAwardRepository awardRepository;
    private final SendAwardMessageEvent sendAwardMessageEvent;
    private final Map<String, IDistributeAward> distributeAwardMap;

    public AwardService(IAwardRepository awardRepository, SendAwardMessageEvent sendAwardMessageEvent, Map<String, IDistributeAward> distributeAwardMap) {
        this.awardRepository = awardRepository;
        this.sendAwardMessageEvent = sendAwardMessageEvent;
        this.distributeAwardMap = distributeAwardMap;
    }

    /**
     * 保存用户奖品流水
     *
     * @param userAwardRecordEntity 用户中奖实体
     */
    @Override
    public void saveUserAwardRecord(UserAwardRecordEntity userAwardRecordEntity) {
        // 1. 构建消息对象
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = this.getSendAwardMessage(userAwardRecordEntity);

        // 2. 创建消息实体
        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> sendAwardMessageEventMessage = sendAwardMessageEvent.buildEventMessage(sendAwardMessage);

        // 3. 构建任务对象
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userAwardRecordEntity.getUserId());
        taskEntity.setTopic(sendAwardMessageEvent.topic());
        taskEntity.setMessageId(sendAwardMessageEventMessage.getId());
        taskEntity.setMessage(sendAwardMessageEventMessage);
        taskEntity.setState(TaskStateVO.create);

        // 4. 构建聚合对象
        UserAwardRecordAggregate userAwardRecordAggregate = UserAwardRecordAggregate.builder()
                .taskEntity(taskEntity)
                .userAwardRecordEntity(userAwardRecordEntity)
                .build();

        // 5. 存储聚合对象 - 一个事务下，用户的中奖记录
        awardRepository.saveUserAwardRecord(userAwardRecordAggregate);
    }
    private SendAwardMessageEvent.SendAwardMessage getSendAwardMessage(UserAwardRecordEntity userAwardRecordEntity) {
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = new SendAwardMessageEvent.SendAwardMessage();
        sendAwardMessage.setUserId(userAwardRecordEntity.getUserId()); // 用户ID
        sendAwardMessage.setOrderId(userAwardRecordEntity.getOrderId()); // 订单ID
        sendAwardMessage.setAwardId(userAwardRecordEntity.getAwardId()); // 奖品ID
        sendAwardMessage.setAwardTitle(userAwardRecordEntity.getAwardTitle()); // 奖品名
        sendAwardMessage.setAwardConfig(userAwardRecordEntity.getAwardConfig()); // 奖品配置
        return sendAwardMessage;
    }

    /**
     * 配送发货奖品
     *
     * @param distributeAwardEntity 分发奖品实体
     */
    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        // 查询奖品Key
        String awardKey = awardRepository.queryAwardKey(distributeAwardEntity.getAwardId());
        if (null == awardKey) {
            log.error("分发奖品，奖品ID不存在。awardKey:{}", awardKey);
            return;
        }
        // 奖品服务
        IDistributeAward distributeAward = distributeAwardMap.get(awardKey);
        if (null == distributeAward) {
            log.error("分发奖品，对应的服务不存在。awardKey:{}", awardKey);
            throw new RuntimeException("分发奖品，奖品" + awardKey + "对应的服务不存在");
        }
        // 发放奖品
        distributeAward.giveOutPrizes(distributeAwardEntity);
    }
}
